<template>
  <div>
    <section>
      <p v-if="title" class="disa">
        <i class="dimension"></i>
        <span style="font-size: 16px; font-weight: 600">{{ title }}</span>
      </p>
      <div class="productsAndServicesCon">
        <div
          class="productsAndServices-con"
          v-for="(item, index) in popularList"
          :key="index"
        >
          <p>{{ item.serverName || "-" }}</p>
          <i class="productsAndServicesTags"></i>
          <div class="con">
            <span>CPU : {{ item.cpu || "无" }}</span>
            <span>内存 : {{ item.memory || "无" }}</span>
            <span>硬盘 : {{ item.hardDisk || "无" }}</span>
            <span>带宽 : {{ item.bandwidth || "无" }}</span>
            <span>防御 : {{ item.defense || "无" }}</span>
            <span>IP : {{ item.ip || "无" }}</span>
            <span>其它 : {{ item.other || "无" }}</span>
          </div>
          <p class="price">
            ¥ {{ item.price || 0
            }}<i class="decorate">/{{ getMode(item.billingMode) }}</i>
          </p>
          <div class="btn">
            <button @click="handleCart(item)">加入购物车</button>
          </div>
        </div>
      </div>
      <el-pagination
        :total="total"
        v-if="total"
        v-model:current-page="queryParams.params.pageNum"
        v-model:page-size="queryParams.params.pageSize"
        layout="->,total, prev, pager, next"
        small
        @size-change="getGovList"
        @current-change="getGovList"
      />
    </section>
    <el-empty v-if="popularList.length == 0" description="暂无数据" />
    <Buy-modal
      v-model:show="buyShow"
      :product="productInfo"
      shoppingCarEnums="GOV_CLOUD"
    />
  </div>
</template>
<script lang="ts" setup>
import { ref, watch } from "vue";
import { govList } from "@/api/zhengWuYun";

const props = defineProps({
  serverTypeId: {
    type: String,
    default: "",
  },
  title: {
    type: String,
    default: "",
  },
});

const popularList = ref<any>([]); // 产品列表
const buyShow = ref(false); // 购买弹窗
const productInfo = ref<any>({}); // 产品信息
const modeList = ref<any>([
  { label: "次", value: 0 },
  { label: "天", value: 1 },
  { label: "月", value: 30 },
  { label: "年", value: 365 },
]);
const total = ref(0); // 总条数
const queryParams = ref<any>({
  serverTypeId: props.serverTypeId,
  params: {
    pageNum: 1,
    pageSize: 10,
  },
});

watch(props, (val) => {
  popularList.value = [];
  queryParams.value.serverTypeId = val.serverTypeId;
  getGovList();
});

// 获取产品列表
const getGovList = () => {
  govList(queryParams.value).then((res: any) => {
    popularList.value = res.rows;
    total.value = res.total;
  });
};

// 获取计费方式
const getMode = (val: any) => {
  let row = modeList.value.find((item: any) => item.value == val);
  return row ? row.label : "";
};

// 打开购物车弹窗
const handleCart = (item: any) => {
  productInfo.value = Object.assign({}, item);
  buyShow.value = true;
};
</script>
<style lang="less" scoped>
.disa {
  width: 100%;
  height: 20px;
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  .dimension {
    display: inline-block;
    margin-right: 4px;
    width: 3px;
    height: 15px;
    background: #4098ff;
  }
}
.productsAndServicesCon {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .btn {
    position: absolute;
    bottom: 30px;
    right: 10px;
    button {
      cursor: pointer;
      border: none;
      width: 104px;
      height: 29px;
    }
    button:nth-child(1) {
      background: #4098ff;
      margin-right: 20px;
      color: #ffffff;
    }
    button:nth-child(2) {
      color: #4098ff;
      background: #ffffff;
      border: 1px solid #4098ff;
    }
  }
  .productsAndServices-con {
    padding: 30px 0 30px 30px;
    margin-bottom: 40px;
    width: 478px;
    height: 201px;
    display: flex;
    background-color: #fff;
    box-shadow: 3px 3px 30px 0px #e3e8f0;
    flex-direction: column;
    justify-content: space-around;
    position: relative;
    transition: all 0.5s;
    &:hover {
      box-shadow: 3px 3px 30px 0px #bcc6d6;
    }
    .con {
      margin-top: 10px;
      color: #909090;
      display: flex;
      flex-wrap: wrap;
      margin-bottom: 30px;
      span {
        margin-right: 45px;
        margin-bottom: 5px;
      }
    }
    .price {
      display: flex;
      // letter-spacing: 0.1em;
      align-items: baseline;
      font-size: 20px;
      font-weight: 500;
      color: #ff3939;
      .decorate {
        font-size: 12px;
        font-weight: 400;
      }
    }
  }
}
</style>
